package com.shanzmoo.admin.manager.store;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.admin.domain.dto.StorePurchasingAgentSaveDTO;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.store.entity.StorePurchasingAgentEntity;
import com.shanzmoo.db.store.entity.query.StorePurchasingAgentQuery;
import com.shanzmoo.db.store.entity.vo.StorePurchasingAgentVO;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.db.store.service.IStorePurchasingAgentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 仓库采购员 管理器
 *
 * @author Gzhao 2021-03-18
 */
@Slf4j
@Component
public class StorePurchasingAgentManager {

    @Autowired
    private IStorePurchasingAgentService storePurchasingAgentService;

    /**
     * 分页查询
     */
    public PageRespBase<StorePurchasingAgentVO> queryPage(StorePurchasingAgentQuery query) {
        Page page = PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<StorePurchasingAgentVO> reList = storePurchasingAgentService.queryPageList(query);

        PageRespBase<StorePurchasingAgentVO> result = PageRespBase.create(page, reList);
        PageHelper.clearPage();
        return result;
    }

    public void add(StorePurchasingAgentSaveDTO saveDto, Integer currentUserId) {
        StorePurchasingAgentEntity entity = saveDto.convertEntity();
        entity.setCreateUser(currentUserId);

        storePurchasingAgentService.save(entity);
    }

    public void updateStatus(Integer id) {
        StorePurchasingAgentEntity entity = storePurchasingAgentService.getById(id);
        entity.setStatus( !entity.getStatus());

        storePurchasingAgentService.updateById(entity);
    }
}
